﻿using Module.Common.Common;
using Module.Common.Properties;
using Module.Common.PubEvent;
using Prism.Events;
using System.Windows.Controls;
using System.Windows.Data;

namespace Module.Common.Views
{
    /// <summary>
    /// 可移动点后台代码
    /// </summary>
    public partial class MovePoint : UserControl
    {
        private bool _isQiPanFanZhuan;
        private bool IsQiPanInvers
        {
            get { return _isQiPanFanZhuan; }
            set
            {
                _isQiPanFanZhuan = value;
            }
        }
        private int Col { get; set; }    // 路径点的列坐标
        private int Row { get; set; }    // 路径点的行坐标
        private bool _canMoveTo;
        public bool CanMoveTo // 棋子是否可移动到此处。可移动到此处时，可接受点击事件，否则，对鼠标透明。
        {
            get { return _canMoveTo; }
            set { _canMoveTo= value; this.IsHitTestVisible = value; }
        }

        public string ModuleName { get; set; }
        private readonly IEventAggregator Ea;

        public MovePoint(IEventAggregator ea)
        {
            InitializeComponent();
            this.Ea = ea;
            CanMoveTo = false;
            PointImage.Visibility = System.Windows.Visibility.Collapsed;

            Ea.GetEvent<SystemMessageEvent>().Subscribe(SystemMessageProcess);
            Ea.GetEvent<ServiceMessageEvent>().Subscribe(
                ServiceMessageProcess,
                ThreadOption.PublisherThread,
                false,
                filter => filter.ModuleName == this.ModuleName);
            Ea.GetEvent<ModuleMessageEvent>().Subscribe(
                ModuleMessageProcess, 
                ThreadOption.UIThread,
                false,
                filter => filter.ModuleName == this.ModuleName);
        }
        #region 消息处理
        private void ServiceMessageProcess(ServiceMessage obj)
        {
            switch (obj.MessageName)
            {
                case "movePointOK": // 得到移动点计算完毕消息后，更新可移动点数据，并显示
                    if (Settings.Default.IsShowMovePoint && obj.MovePoint_Message[Col, Row] == true) { PointImage.Visibility = System.Windows.Visibility.Visible;  }
                    else { PointImage.Visibility = System.Windows.Visibility.Collapsed; }
                    CanMoveTo = obj.MovePoint_Message[Col, Row];
                    break;
                default: break;
            }
        }

        private void SystemMessageProcess(SystemMessage obj)
        {
            switch (obj.MessageName)
            {
                
                case "qipanInverse":  // 棋盘翻转消息
                    IsQiPanInvers = !IsQiPanInvers;
                    SetPosition(Col, Row);
                    break;
                default:
                    break;
            }
        }
        /// <summary>
        /// 订阅消息处理
        /// </summary>
        /// <param name="obj">消息体</param>
        private void ModuleMessageProcess(ModuleMessage obj)
        {
            switch (obj.MessageName)
            {
                case "qiziStartMove":    // 有棋子移动动画开始后
                    PointImage.Visibility = System.Windows.Visibility.Collapsed;
                    CanMoveTo = false;
                    break;
                case "pointGetClicked":  // 有移动点被点击消息
                    PointImage.Visibility = System.Windows.Visibility.Collapsed;
                    CanMoveTo = false;
                    break;
                case "reStart": // 重新开局
                    PointImage.Visibility = System.Windows.Visibility.Hidden;
                    CanMoveTo = false;
                    break;
                default: break;
            }
        }
        #endregion

        /// <summary>
        /// 棋子移动目的地标记类，
        /// 在棋子可移动到的有效位置，设置标记。
        /// 点击此标记时，所选棋子移动到此标记位置。
        /// </summary>
        /// <param name="x">列位置</param>
        /// <param name="y">行位置</param>
        public void SetInitPosition(int x, int y)
        {
            Col = x;
            Row = y;
            SetValue(Canvas.LeftProperty, GlobalData.GetQiPanGrid_X(x) + 3);
            SetValue(Canvas.TopProperty, GlobalData.GetQiPanGrid_Y(y) + 3);

        }
        /// <summary>
        /// 可移动点选中时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnClick(object sender, System.Windows.RoutedEventArgs e)
        {
            Ea.GetEvent<ModuleMessageEvent>().Publish(new ModuleMessage()
            {
                MessageName = "pointGetClicked",
                SenderFrom = "MovePoint.xaml.cs",
                NewCol = Col,
                NewRow = Row,
                Content = $"【{this.GetType().Name}】：移动点被点击",
                ModuleName=this.ModuleName,
            });
        }

        /// <summary>
        /// 设置可移动点位置
        /// </summary>
        /// <param name="x">行</param>
        /// <param name="y">列</param>
        private void SetPosition(int x, int y)
        {

            if (IsQiPanInvers) // 如果棋盘翻转为上红下黑，则进行坐标转换
            {
                x = 8 - x;
                y = 9 - y;
            }
            SetValue(Canvas.LeftProperty, GlobalData.GetQiPanGrid_X(x) + 3);
            SetValue(Canvas.TopProperty, GlobalData.GetQiPanGrid_Y(y) + 3);
        }

        private void OnClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (CanMoveTo)
            {
                Ea.GetEvent<ModuleMessageEvent>().Publish(new ModuleMessage()
                {
                    MessageName = "pointGetClicked",
                    SenderFrom = "MovePoint.xaml.cs",
                    NewCol = Col,
                    NewRow = Row,
                    Content = $"【{this.GetType().Name}】：移动点被点击",
                    ModuleName = this.ModuleName,
                });
            }
        }
    }
}
